package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.assistant.UserAssistantSuit;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class AssistantSuitDaoImpl extends TempDataObjectDao<UserAssistantSuit> {
    public static String getAssistantSuitSql = "select * from `t_u_assistant_suit` where `userId`=? ";

    public Map<Integer, UserAssistantSuit> getAssistantMap(long userId) {
        Map<Integer, UserAssistantSuit> assistantSuitMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAssistantSuitSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                assistantSuitMap.put(rs.getInt("suitId"), parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return assistantSuitMap;
    }

    public static String updateAssistantSuitSql = "update `t_u_assistant_suit` set suitLevel=? where userId=? and suitId=?";

    @Override
    protected boolean updateData(UserAssistantSuit userAssistantSuit) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantSuit.getSuitLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantSuit.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantSuit.getSuitId()));
        if (getDb().executeNoneQuery(updateAssistantSuitSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertAssistantSuitSql = "insert into `t_u_assistant_suit`(userId,suitId,suitLevel) values (?,?,?)";

    @Override
    protected boolean addData(UserAssistantSuit userAssistantSuit) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userAssistantSuit.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantSuit.getSuitId()));
        para.put(i++, new DbParameter(Types.INTEGER, userAssistantSuit.getSuitLevel()));
        if (getDb().executeNoneQuery(insertAssistantSuitSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserAssistantSuit parseData(ResultSet rs) throws SQLException {
        UserAssistantSuit userAssistantSuit = new UserAssistantSuit();
        userAssistantSuit.setUserId(rs.getLong("userId"));
        userAssistantSuit.setSuitId(rs.getInt("suitId"));
        userAssistantSuit.setSuitLevel(rs.getInt("suitLevel"));
        return userAssistantSuit;
    }
}
